<?php

class VipAction extends CommAction
{
    /**
     * 首页
     */
    public function index(){  
        
       
        $userModel = D('Wxuser');
        $openid = Session::get('openid');
        $id = $userModel->getIdByOpenId($openid);
        $vipModel=D('VipList');
        $vipInfo=$vipModel->where(array('openid'=>$openid))->find();
        if(empty($vipInfo)){

            $rs['time']=0;
            $rs['mobile']='未设置';
            $rs['name']='未设置';
            $rs['point']=0;
        }else{
            $rs['time']=round((time()-$vipInfo['createtime'])/3600,1);
            $rs['mobile']=$vipInfo['mobile'];
            $rs['name']=$vipInfo['name'];
            $rs['point']=$vipInfo['points'];
        }
        $rs['nickname']=$userModel->where(array('openid'=>$openid))->getField('nickname');
        $rs['imgurl']=$userModel->where(array('openid'=>$openid))->getField('imgurl');
        $this->assign('rs',$rs);

        //找出已领取的优惠券数量
        $cardModel=D('Card');
        $cardCount=$cardModel->where(array('openid'=>$openid))->count();
        $this->assign('cardCount',$cardCount);
        $this->assign('vipInfo',$vipInfo);


        //未读消息数
        $mesModel=D('Message');
        $map=array();
        $map['openid']=session::get('openid');
        $map['read']=0;
        $count=$mesModel->where($map)->count();
        $this->assign('messageCount',$count);
        $this->display();

    }


   public function regist(){
       if(IS_POST){
           $verify=trim(I('post.verify'));
           $mobile=trim(I('post.mobile'));
           $name=trim(I('post.name'));
           $smsModel=D('Sms');
           $map=array();
           $map['openid']=session::get('openid');
           $map['mobile']=$mobile;
           $map['createtime']=array('gt',time()-600);
           $content=$smsModel->where($map)->order('createtime desc')->getField('msg');
           if($content!=$verify){
               $this->simpleAlert('手机验证码错误');
           }else{
               $map=array();
               $map['openid']=session::get('openid');
               $exist=D('VipList')->where($map)->find();
               if($exist){
                   $data=array();
                   $data['name']=$name;
                   $data['mobile']=$mobile;
                   D('VipList')->where($map)->data($data)->save();
                   $this->simpleAlert('信息修改成功',U('Vip/index'));
               }else{
                   $data=array();
                   $data['vipid']=$this->getVipid();
                   $data['openid']=session::get('openid');
                   $data['points']=0;
                   $data['balance']=0;
                   $data['total']=0;
                   $data['viplevel']=1;
                   $data['createtime']=time();
                   $data['name']=$name;
                   $data['mobile']=$mobile;
                   D('VipList')->data($data)->add();
                   $this->simpleAlert('恭喜您注册成功！',U('Vip/index'));
               }


           }

       }
   }

  

    
}
?>